const commonModule = require("./common")
const mysqlUtil = require("../utils/mysqlUtil")
const redisUtil = require('../utils/redisUtil')
const logger = require("../utils/logger")
const util = require("../utils/util")

const moment = require('moment')

// 生成每个用户的打卡记录表 从现在到下个月末 并根据节假日的表 设置其上班状态
async function shengchengShangbanSta1 () {
    try {
        // 查询所有在职用户
        let res = await mysqlUtil.execute('SELECT * FROM tb_user WHERE sta != "离职"')
        let allUsers = res
        // 计算从现在到下个月末的天数
        let diffDays = moment().add(1, 'month').endOf('month').diff(moment(), 'day')
        // 查询从现在到下个月末的节假日情况
        let sqlParams = [moment().format('YYYY-MM-DD'), moment().add(diffDays, 'day').format('YYYY-MM-DD')]
        res = await mysqlUtil.execute('SELECT riqi, typ FROM con_jiejiari WHERE riqi >= ? AND riqi <= ?', sqlParams)
        let jiejiariInfo = {}
        for (let i = 0; i != res.length; i++) {
            res[i].riqi = moment(res[i].riqi).format('YYYY-MM-DD')
            if (res[i].typ == 0) { // 上班
                jiejiariInfo[res[i].riqi] = {id: 1, desc: '上班'}
            } else if (res[i].typ == 1 || res[i].typ == 2) { // 休息
                jiejiariInfo[res[i].riqi] = {id: 0, desc: '休息'}
            }
        }
        // 添加考勤记录
        for (let i = 0; i != allUsers.length; i++) {
            let userInfo = allUsers[i]
            for (let j = 0; j != diffDays; j++) {
                let dayStr = moment().add(j, 'day').format('YYYY-MM-DD')
                let dakaInfo = {
                    uid: userInfo.uid,
                    day: dayStr,
                    sta: JSON.stringify(jiejiariInfo[dayStr] || {})
                }
                await mysqlUtil.execute('INSERT INTO tb_user_daka SET ? ON DUPLICATE KEY UPDATE sta = ?', [dakaInfo, dakaInfo.sta])
            }
        }
        logger.info('生成上班状态完成1111')
        return {code: 0, message: ''}
    } catch (err) {
        logger.error(err)
        return {code: -1, message: 'system error'}
    }
}

module.exports = {
    dubandanApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        logger.debug('add dubandan', params)

        let auditorsObj = {}
        for (let i = 0; i != params.auditors.length; i++) {
            auditorsObj['auditor' + i] = { uid: params.auditors[i].uid, stepName: params.auditors[i].stepName, sta: 0 }
        }

        try {
            let res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
            if (res.length == 0) return callback({code: -1, message: '玩家不存在'})
            let myName = res[0].name

            let sql = 'INSERT INTO tb_user_duban_apply SET ?'
            let sqlParams = [{
                uid,
                title: params.title,
                content: params.content,
                // dutyDepart: params.dutyDepartment,
                dutyInfo: JSON.stringify(params.dutyInfo || []),
                dubanDepart: params.dubanDepartment,
                dubanTime: params.dubanTime,
                deadTime: params.deadTime,
                dutyOfficer: params.dutyOfficer,
                auditSta: 1,
                sta: 0,
                pics: JSON.stringify(params.pics || []),
                attaches: JSON.stringify(params.attaches || []),
                auditors: JSON.stringify(auditorsObj),
                createTime: Date.now()
            }]
            res = await mysqlUtil.execute(sql, sqlParams)

            let applyId = res.insertId

            // 为审核人员添加任务
            await mysqlUtil.execute('INSERT INTO tb_user_task SET ?', [{
                uid: params.auditors[0].uid,
                typ: 6,
                relevenceId: applyId,
                pos: 0,
                title: '督办申请',
                content: myName+ '的督办申请需要您审批',
                sta: 0,
                createTime: Date.now()
            }])
            io.in(params.auditors[0].uid + '-app').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-app').emit('TASK-NEW')
            io.in(params.auditors[0].uid + '-web').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-web').emit('TASK-NEW')

            util.sendReminderSmsToUser(params.auditors[0].uid, '督办申请', '待审批')

            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 撤销督办单申请
    revertDubandanApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc
        if (!params.id) return callback({code: -1, message: '参数缺失'})

        try {
            let res = await mysqlUtil.execute('UPDATE tb_user_duban_apply SET chexiao = 1 WHERE id = ? LIMIT 1', [params.id])
            if (res.affectedRows == 0) return ({code: -1, message: '撤销失败'})
            // 将审核人员的任务状态改下 并通知前端
            res = await mysqlUtil.execute('SELECT id, uid FROM tb_user_task WHERE typ = 6 AND relevenceId = ?', [params.id])
            for (let i = 0; i != res.length; i++) {
                await mysqlUtil.execute('UPDATE tb_user_task SET sta = 3 WHERE id = ? LIMIT 1', [res[i].id])
                // 通知前端
                io.in(res[i].uid + '-app').emit('TASK-REVERT', res[i].id)
                io.in(res[i].uid + '-web').emit('TASK-REVERT', res[i].id)
            }
            callback({code: 0, message: ''})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 加班申请记录
    dubandanApplyRecord: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        // 小于的id
        let lessThan = params.lessThan || 0
        // 大于的id
        let greaterThan = params.greaterThan || 0
        // 每页的数量
        let pageCount = params.pageCount || 10

        try {
            let sql = ''
            let sqlParams = []
            if (lessThan) {
                sql = 'SELECT * FROM tb_user_duban_apply WHERE uid = ? AND id < ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, lessThan, pageCount]
            } else if (greaterThan) {
                sql = 'SELECT * FROM tb_user_duban_apply WHERE uid = ? AND id > ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, greaterThan, pageCount]
            } else {
                sql = 'SELECT * FROM tb_user_duban_apply WHERE uid = ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, pageCount]
            }
            let res = await mysqlUtil.execute(sql, sqlParams)
            callback({code: 0, message: 'success', data: res})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    dubandanApplyRecordWeb: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        let pageIndex = parseInt(params.pageIndex || 1);
        let pageCount = parseInt(params.pageCount || 10);

        try {
            let res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_user_duban_apply WHERE uid = ?', [uid])
            let count = res[0].cnt
            let sql = `
                SELECT
                    tb_user_duban_apply.*,
                    tb_user.name dutyOfficerName
                FROM tb_user_duban_apply 
                INNER JOIN tb_user ON tb_user.uid = tb_user_duban_apply.dutyOfficer
                WHERE tb_user_duban_apply.uid = ? ORDER BY tb_user_duban_apply.id DESC LIMIT ?, ?
            `
            let params = [uid, (pageIndex - 1) * pageCount, pageCount]
            res = await mysqlUtil.execute(sql, params)
            callback({code: 0, message: 'success', data: {list: res, count}})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    dubandanApplyDetail: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        if (!params.id) return callback({code: -1, message: '参数错误'})

        try {
            let res = await mysqlUtil.execute('SELECT * FROM tb_user_duban_apply WHERE id = ? LIMIT 1', [params.id])
            if (res.length == 0) return callback({code: -1, message: '不存在'})
            let applyInfo = res[0]
            applyInfo.auditors = applyInfo.auditors || []
            for (let key in applyInfo.auditors) {
                let sql = `
                    SELECT
                        con_position.name
                    FROM
                        tb_user
                    INNER JOIN con_position ON con_position.id = tb_user.position
                    WHERE tb_user.uid = ?
                    LIMIT 1
                `
                let r = await mysqlUtil.execute(sql, [applyInfo.auditors[key].uid])
                if (r.length > 0) {
                    applyInfo.auditors[key].positionName = r[0].name
                }
            }
            callback({code: 0, message: '', data: applyInfo})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    meetingRoomApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        logger.debug('meetingRoomApply', params)

        let auditorsObj = {}
        for (let i = 0; i != params.auditors.length; i++) {
            auditorsObj['auditor' + i] = { uid: params.auditors[i].uid, stepName: params.auditors[i].stepName, sta: 0 }
        }

        try {

            let res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
            if (res.length == 0) return callback({code: -1, message: '玩家不存在'})
            let myName = res[0].name

            let sql = 'INSERT INTO tb_meeting_room_apply SET ?'
            let sqlParams = [{
                uid,
                sta: 0,
                auditSta: 1,
                department: params.department,
                startTime: params.startTime,
                endTime: params.endTime,
                room: params.room,
                topic: params.topic || '',
                renshu: params.renshu || 0,
                remark: params.remark || '',
                equips: JSON.stringify(params.equips),
                items: JSON.stringify(params.items),
                attenders: JSON.stringify(params.attenders),
                auditors: JSON.stringify(auditorsObj),
                createTime: Date.now()
            }]
            res = await mysqlUtil.execute(sql, sqlParams)
            let applyId = res.insertId

            // 为审核人员添加任务
            await mysqlUtil.execute('INSERT INTO tb_user_task SET ?', [{
                uid: params.auditors[0].uid,
                typ: 7,
                relevenceId: applyId,
                pos: 0,
                title: '会议室申请',
                content: myName+ '的会议室申请需要您审批',
                sta: 0,
                createTime: Date.now()
            }])
            io.in(params.auditors[0].uid + '-app').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-app').emit('TASK-NEW')
            io.in(params.auditors[0].uid + '-web').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-web').emit('TASK-NEW')

            util.sendReminderSmsToUser(params.auditors[0].uid, '会议室申请', '待审批')

            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }

        callback({code: 0, message: 'success'})
    },

    // 撤销督办单申请
    revertMeetingRoomApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc
        if (!params.id) return callback({code: -1, message: '参数缺失'})

        try {
            let res = await mysqlUtil.execute('UPDATE tb_meeting_room_apply SET chexiao = 1 WHERE id = ? LIMIT 1', [params.id])
            if (res.affectedRows == 0) return ({code: -1, message: '撤销失败'})
            // 将审核人员的任务状态改下 并通知前端
            res = await mysqlUtil.execute('SELECT id, uid FROM tb_user_task WHERE typ = 7 AND relevenceId = ?', [params.id])
            for (let i = 0; i != res.length; i++) {
                await mysqlUtil.execute('UPDATE tb_user_task SET sta = 3 WHERE id = ? LIMIT 1', [res[i].id])
                // 通知前端
                io.in(res[i].uid + '-app').emit('TASK-REVERT', res[i].id)
                io.in(res[i].uid + '-web').emit('TASK-REVERT', res[i].id)
            }
            callback({code: 0, message: ''})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 加班申请记录
    meetingRoomApplyRecord: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        // 小于的id
        let lessThan = params.lessThan || 0
        // 大于的id
        let greaterThan = params.greaterThan || 0
        // 每页的数量
        let pageCount = params.pageCount || 10

        try {
            let sql = ''
            let sqlParams = []
            if (lessThan) {
                sql = 'SELECT * FROM tb_meeting_room_apply WHERE uid = ? AND id < ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, lessThan, pageCount]
            } else if (greaterThan) {
                sql = 'SELECT * FROM tb_meeting_room_apply WHERE uid = ? AND id > ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, greaterThan, pageCount]
            } else {
                sql = 'SELECT * FROM tb_meeting_room_apply WHERE uid = ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, pageCount]
            }
            let res = await mysqlUtil.execute(sql, sqlParams)
            callback({code: 0, message: 'success', data: res})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    meetingRoomApplyRecordWeb: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        let pageIndex = parseInt(params.pageIndex || 1);
        let pageCount = parseInt(params.pageCount || 10);

        try {
            let res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_meeting_room_apply WHERE uid = ?', [uid])
            let count = res[0].cnt
            let sql = 'SELECT * FROM tb_meeting_room_apply WHERE uid = ? ORDER BY id DESC LIMIT ?, ?'
            let params = [uid, (pageIndex - 1) * pageCount, pageCount]
            res = await mysqlUtil.execute(sql, params)
            callback({code: 0, message: 'success', data: {list: res, count}})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    meetingRoomApplyDetail: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        if (!params.id) return callback({code: -1, message: '参数错误'})

        try {
            let res = await mysqlUtil.execute('SELECT * FROM tb_meeting_room_apply WHERE id = ? LIMIT 1', [params.id])
            if (res.length == 0) return callback({code: -1, message: '不存在'})
            let applyInfo = res[0]
            applyInfo.auditors = applyInfo.auditors || []
            for (let key in applyInfo.auditors) {
                let sql = `
                    SELECT
                        con_position.name
                    FROM
                        tb_user
                    INNER JOIN con_position ON con_position.id = tb_user.position
                    WHERE tb_user.uid = ?
                    LIMIT 1
                `
                let r = await mysqlUtil.execute(sql, [applyInfo.auditors[key].uid])
                if (r.length > 0) {
                    applyInfo.auditors[key].positionName = r[0].name
                }
            }
            callback({code: 0, message: '', data: applyInfo})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    jiedaiApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        logger.debug('jiedaiApply', params)

        let auditorsObj = {}
        for (let i = 0; i != params.auditors.length; i++) {
            auditorsObj['auditor' + i] = { uid: params.auditors[i].uid, stepName: params.auditors[i].stepName, sta: 0 }
        }

        try {

            let res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
            if (res.length == 0) return callback({code: -1, message: '玩家不存在'})
            let myName = res[0].name

            let sql = 'INSERT INTO tb_user_jiedai_apply SET ?'
            let sqlParams = [{
                uid,
                sta: 0,
                auditSta: 1,
                department: params.department,
                startTime: params.startTime,
                endTime: params.endTime,
                guestCompany: params.guestCompany,
                guestCnt: params.guestCnt,
                peitongrenshu: params.peitongrenshu,
                reason: params.reason,
                remark: params.remark || '',
                attaches: JSON.stringify(params.attaches || []),
                auditors: JSON.stringify(auditorsObj),
                peitongrenyuan: JSON.stringify(params.peitongrenyuan),
                dabaoren: JSON.stringify(params.dabaoren),
                createTime: Date.now()
            }]
            res = await mysqlUtil.execute(sql, sqlParams)

            let applyId = res.insertId

            // 为审核人员添加任务
            await mysqlUtil.execute('INSERT INTO tb_user_task SET ?', [{
                uid: params.auditors[0].uid,
                typ: 8,
                relevenceId: applyId,
                pos: 0,
                title: '接待单申请',
                content: myName + '的接待单申请需要您审批',
                sta: 0,
                createTime: Date.now()
            }])
            io.in(params.auditors[0].uid + '-app').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-app').emit('TASK-NEW')
            io.in(params.auditors[0].uid + '-web').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-web').emit('TASK-NEW')

            util.sendReminderSmsToUser(params.auditors[0].uid, '接待单申请', '待审批')

            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 撤销督办单申请
    revertJiedaiApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc
        if (!params.id) return callback({code: -1, message: '参数缺失'})

        try {
            let res = await mysqlUtil.execute('UPDATE tb_user_jiedai_apply SET chexiao = 1 WHERE id = ? LIMIT 1', [params.id])
            if (res.affectedRows == 0) return ({code: -1, message: '撤销失败'})
            // 将审核人员的任务状态改下 并通知前端
            res = await mysqlUtil.execute('SELECT id, uid FROM tb_user_task WHERE typ = 8 AND relevenceId = ?', [params.id])
            for (let i = 0; i != res.length; i++) {
                await mysqlUtil.execute('UPDATE tb_user_task SET sta = 3 WHERE id = ? LIMIT 1', [res[i].id])
                // 通知前端
                io.in(res[i].uid + '-app').emit('TASK-REVERT', res[i].id)
                io.in(res[i].uid + '-web').emit('TASK-REVERT', res[i].id)
            }
            callback({code: 0, message: ''})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 加班申请记录
    jiedaiApplyRecord: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        // 小于的id
        let lessThan = params.lessThan || 0
        // 大于的id
        let greaterThan = params.greaterThan || 0
        // 每页的数量
        let pageCount = params.pageCount || 10

        try {
            let sql = ''
            let sqlParams = []
            if (lessThan) {
                sql = 'SELECT * FROM tb_user_jiedai_apply WHERE uid = ? AND id < ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, lessThan, pageCount]
            } else if (greaterThan) {
                sql = 'SELECT * FROM tb_user_jiedai_apply WHERE uid = ? AND id > ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, greaterThan, pageCount]
            } else {
                sql = 'SELECT * FROM tb_user_jiedai_apply WHERE uid = ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, pageCount]
            }
            let res = await mysqlUtil.execute(sql, sqlParams)
            callback({code: 0, message: 'success', data: res})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    jiedaiApplyRecordWeb: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        let pageIndex = parseInt(params.pageIndex || 1);
        let pageCount = parseInt(params.pageCount || 10);

        try {
            let res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_user_jiedai_apply WHERE uid = ?', [uid])
            let count = res[0].cnt
            let sql = 'SELECT * FROM tb_user_jiedai_apply WHERE uid = ? ORDER BY id DESC LIMIT ?, ?'
            let params = [uid, (pageIndex - 1) * pageCount, pageCount]
            res = await mysqlUtil.execute(sql, params)
            callback({code: 0, message: 'success', data: {list: res, count}})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    jiedaiApplyDetail: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        if (!params.id) return callback({code: -1, message: '参数错误'})

        try {
            let res = await mysqlUtil.execute('SELECT * FROM tb_user_jiedai_apply WHERE id = ? LIMIT 1', [params.id])
            if (res.length == 0) return callback({code: -1, message: '不存在'})
            let applyInfo = res[0]
            applyInfo.auditors = applyInfo.auditors || []
            for (let key in applyInfo.auditors) {
                let sql = `
                    SELECT
                        con_position.name
                    FROM
                        tb_user
                    INNER JOIN con_position ON con_position.id = tb_user.position
                    WHERE tb_user.uid = ?
                    LIMIT 1
                `
                let r = await mysqlUtil.execute(sql, [applyInfo.auditors[key].uid])
                if (r.length > 0) {
                    applyInfo.auditors[key].positionName = r[0].name
                }
            }
            callback({code: 0, message: '', data: applyInfo})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    jiayouApply: async (io, socket, params, callback) => {

        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        logger.debug('jiayou apply', params)

        let auditorsObj = {}
        for (let i = 0; i != params.auditors.length; i++) {
            auditorsObj['auditor' + i] = { uid: params.auditors[i].uid, stepName: params.auditors[i].stepName, sta: 0 }
        }

        try {
            let res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
            if (res.length == 0) return callback({code: -1, message: '玩家不存在'})
            let myName = res[0].name

            let sql = 'INSERT INTO tb_user_jiayou_apply SET ?'
            let sqlParams = [{
                uid,
                sta: 0,
                auditSta: 1,
                department: params.department,
                jiayouDate: params.jiayouDate,
                chepai: params.chepai,
                licheng: params.licheng,
                lastLicheng: params.lastLicheng,
                pics: JSON.stringify(params.pics),
                wanzheng: params.wanzheng || 0,
                auditors: JSON.stringify(auditorsObj),
                createTime: Date.now()
            }]
            res = await mysqlUtil.execute(sql, sqlParams)

            let applyId = res.insertId

            // 为审核人员添加任务
            await mysqlUtil.execute('INSERT INTO tb_user_task SET ?', [{
                uid: params.auditors[0].uid,
                typ: 9,
                relevenceId: applyId,
                pos: 0,
                title: '加油申请',
                content: myName+ '的加油申请需要您审批',
                sta: 0,
                createTime: Date.now()
            }])
            io.in(params.auditors[0].uid + '-app').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-app').emit('TASK-NEW')
            io.in(params.auditors[0].uid + '-web').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-web').emit('TASK-NEW')

            util.sendReminderSmsToUser(params.auditors[0].uid, '加油申请', '待审批')

            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 撤销督办单申请
    revertJiayouApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc
        if (!params.id) return callback({code: -1, message: '参数缺失'})

        try {
            let res = await mysqlUtil.execute('UPDATE tb_user_jiayou_apply SET chexiao = 1 WHERE id = ? LIMIT 1', [params.id])
            if (res.affectedRows == 0) return ({code: -1, message: '撤销失败'})
            // 将审核人员的任务状态改下 并通知前端
            res = await mysqlUtil.execute('SELECT id, uid FROM tb_user_task WHERE typ = 9 AND relevenceId = ?', [params.id])
            for (let i = 0; i != res.length; i++) {
                await mysqlUtil.execute('UPDATE tb_user_task SET sta = 3 WHERE id = ? LIMIT 1', [res[i].id])
                // 通知前端
                io.in(res[i].uid + '-app').emit('TASK-REVERT', res[i].id)
                io.in(res[i].uid + '-web').emit('TASK-REVERT', res[i].id)
            }
            callback({code: 0, message: ''})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 加班申请记录
    jiayouApplyRecord: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        // 小于的id
        let lessThan = params.lessThan || 0
        // 大于的id
        let greaterThan = params.greaterThan || 0
        // 每页的数量
        let pageCount = params.pageCount || 10

        try {
            let sql = ''
            let sqlParams = []
            if (lessThan) {
                sql = 'SELECT * FROM tb_user_jiayou_apply WHERE uid = ? AND id < ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, lessThan, pageCount]
            } else if (greaterThan) {
                sql = 'SELECT * FROM tb_user_jiayou_apply WHERE uid = ? AND id > ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, greaterThan, pageCount]
            } else {
                sql = 'SELECT * FROM tb_user_jiayou_apply WHERE uid = ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, pageCount]
            }
            let res = await mysqlUtil.execute(sql, sqlParams)
            callback({code: 0, message: 'success', data: res})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    jiayouApplyRecordWeb: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        let pageIndex = parseInt(params.pageIndex || 1);
        let pageCount = parseInt(params.pageCount || 10);

        try {
            let res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_user_jiayou_apply WHERE uid = ?', [uid])
            let count = res[0].cnt
            let sql = 'SELECT * FROM tb_user_jiayou_apply WHERE uid = ? ORDER BY id DESC LIMIT ?, ?'
            let params = [uid, (pageIndex - 1) * pageCount, pageCount]
            res = await mysqlUtil.execute(sql, params)
            callback({code: 0, message: 'success', data: {list: res, count}})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    jiayouApplyDetail: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        if (!params.id) return callback({code: -1, message: '参数错误'})

        try {
            let res = await mysqlUtil.execute('SELECT * FROM tb_user_jiayou_apply WHERE id = ? LIMIT 1', [params.id])
            if (res.length == 0) return callback({code: -1, message: '不存在'})
            let applyInfo = res[0]
            applyInfo.auditors = applyInfo.auditors || []
            for (let key in applyInfo.auditors) {
                let sql = `
                    SELECT
                        con_position.name
                    FROM
                        tb_user
                    INNER JOIN con_position ON con_position.id = tb_user.position
                    WHERE tb_user.uid = ?
                    LIMIT 1
                `
                let r = await mysqlUtil.execute(sql, [applyInfo.auditors[key].uid])
                if (r.length > 0) {
                    applyInfo.auditors[key].positionName = r[0].name
                }
            }
            callback({code: 0, message: '', data: applyInfo})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    yongcheApply: async (io, socket, params, callback) => {

        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        logger.debug('yongche apply', params)

        let auditorsObj = {}
        for (let i = 0; i != params.auditors.length; i++) {
            auditorsObj['auditor' + i] = { uid: params.auditors[i].uid, stepName: params.auditors[i].stepName, sta: 0 }
        }

        try {

            // 先加个1秒钟的锁
            let lockKey = 'LOCK:YONGCHE:APPLY:' + uid
            await redisUtil.setLock(lockKey, 2)

            let res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
            if (res.length == 0) return callback({code: -1, message: '玩家不存在'})
            let myName = res[0].name

            let sql = 'INSERT INTO tb_user_yongche_apply SET ?'
            let sqlParams = [{
                uid,
                sta: 0,
                auditSta: 1,
                department: params.department,
                jiashiren: params.jiashiren || 0,
                jiashirenName: params.jiashirenName || '',
                chengzuoren: JSON.stringify(params.chengzuoren || []),
                startTime: params.startTime,
                endTime: params.endTime,
                chepai: params.chepai,
                reason: params.reason,
                routes: params.routes,
                attaches: JSON.stringify(params.attaches || []),
                auditors: JSON.stringify(auditorsObj),
                createTime: Date.now()
            }]
            res = await mysqlUtil.execute(sql, sqlParams)

            let applyId = res.insertId

            // 为审核人员添加任务
            await mysqlUtil.execute('INSERT INTO tb_user_task SET ?', [{
                uid: params.auditors[0].uid,
                typ: 10,
                relevenceId: applyId,
                pos: 0,
                title: '用车申请',
                content: myName+ '的用车申请需要您审批',
                sta: 0,
                createTime: Date.now()
            }])
            io.in(params.auditors[0].uid + '-app').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-app').emit('TASK-NEW')
            io.in(params.auditors[0].uid + '-web').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-web').emit('TASK-NEW')

            util.sendReminderSmsToUser(params.auditors[0].uid, '用车申请', '待审批')

            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 撤销督办单申请
    revertYongcheApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc
        if (!params.id) return callback({code: -1, message: '参数缺失'})

        try {
            let res = await mysqlUtil.execute('UPDATE tb_user_yongche_apply SET chexiao = 1 WHERE id = ? LIMIT 1', [params.id])
            if (res.affectedRows == 0) return ({code: -1, message: '撤销失败'})
            // 将审核人员的任务状态改下 并通知前端
            res = await mysqlUtil.execute('SELECT id, uid FROM tb_user_task WHERE typ = 10 AND relevenceId = ?', [params.id])
            for (let i = 0; i != res.length; i++) {
                await mysqlUtil.execute('UPDATE tb_user_task SET sta = 3 WHERE id = ? LIMIT 1', [res[i].id])
                // 通知前端
                io.in(res[i].uid + '-app').emit('TASK-REVERT', res[i].id)
                io.in(res[i].uid + '-web').emit('TASK-REVERT', res[i].id)
            }
            callback({code: 0, message: ''})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 加班申请记录
    yongcheApplyRecord: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        // 小于的id
        let lessThan = params.lessThan || 0
        // 大于的id
        let greaterThan = params.greaterThan || 0
        // 每页的数量
        let pageCount = params.pageCount || 10

        try {
            let sql = ''
            let sqlParams = []
            if (lessThan) {
                sql = 'SELECT * FROM tb_user_yongche_apply WHERE uid = ? AND id < ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, lessThan, pageCount]
            } else if (greaterThan) {
                sql = 'SELECT * FROM tb_user_yongche_apply WHERE uid = ? AND id > ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, greaterThan, pageCount]
            } else {
                sql = 'SELECT * FROM tb_user_yongche_apply WHERE uid = ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, pageCount]
            }
            let res = await mysqlUtil.execute(sql, sqlParams)
            callback({code: 0, message: 'success', data: res})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    yongcheApplyRecordWeb: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        let pageIndex = parseInt(params.pageIndex || 1);
        let pageCount = parseInt(params.pageCount || 10);

        try {
            let res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_user_yongche_apply WHERE uid = ?', [uid])
            let count = res[0].cnt
            let sql = 'SELECT * FROM tb_user_yongche_apply WHERE uid = ? ORDER BY id DESC LIMIT ?, ?'
            let params = [uid, (pageIndex - 1) * pageCount, pageCount]
            res = await mysqlUtil.execute(sql, params)
            callback({code: 0, message: 'success', data: {list: res, count}})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    yongcheApplyDetail: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        if (!params.id) return callback({code: -1, message: '参数错误'})

        try {
            let res = await mysqlUtil.execute('SELECT * FROM tb_user_yongche_apply WHERE id = ? LIMIT 1', [params.id])
            if (res.length == 0) return callback({code: -1, message: '不存在'})
            let applyInfo = res[0]
            applyInfo.auditors = applyInfo.auditors || []
            for (let key in applyInfo.auditors) {
                let sql = `
                    SELECT
                        con_position.name
                    FROM
                        tb_user
                    INNER JOIN con_position ON con_position.id = tb_user.position
                    WHERE tb_user.uid = ?
                    LIMIT 1
                `
                let r = await mysqlUtil.execute(sql, [applyInfo.auditors[key].uid])
                if (r.length > 0) {
                    applyInfo.auditors[key].positionName = r[0].name
                }
            }
            callback({code: 0, message: '', data: applyInfo})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 用印申请
    yongyinApply: async (io, socket, params, callback) => {

        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        logger.debug('yongyin apply', params)

        let auditorsObj = {}
        for (let i = 0; i != params.auditors.length; i++) {
            auditorsObj['auditor' + i] = { uid: params.auditors[i].uid, stepName: params.auditors[i].stepName, sta: 0 }
        }

        try {
            let res = await mysqlUtil.execute('SELECT name FROM tb_user WHERE uid = ? LIMIT 1', [uid])
            if (res.length == 0) return callback({code: -1, message: '玩家不存在'})
            let myName = res[0].name

            // 查询本月已经申请多少了
            res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_user_yongyin_apply WHERE createTime > ?', [moment().startOf('month').valueOf()])
            let cnt = res[0].cnt + 1
            let bianhao = moment().format('YYYYMM')
            if (cnt < 10) bianhao = bianhao + '00' + cnt
            else if (cnt < 100) bianhao = bianhao + '0' + cnt
            else bianhao = bianhao + cnt

            let sql = 'INSERT INTO tb_user_yongyin_apply SET ?'
            let sqlParams = [{
                uid,
                sta: 0,
                auditSta: 1,
                bianhao,
                department: params.department,
                yongyinTime: params.yongyinTime,
                wenjianming: params.wenjianming,
                fenshu: params.fenshu,
                reason: params.reason,
                jine: params.jine,
                yinzhang: JSON.stringify(params.yinzhang || []),
                pics: JSON.stringify(params.pics || []),
                attaches: JSON.stringify(params.attaches || []),
                auditors: JSON.stringify(auditorsObj),
                createTime: Date.now()
            }]
            res = await mysqlUtil.execute(sql, sqlParams)

            let applyId = res.insertId

            // 为审核人员添加任务
            await mysqlUtil.execute('INSERT INTO tb_user_task SET ?', [{
                uid: params.auditors[0].uid,
                typ: 11,
                relevenceId: applyId,
                pos: 0,
                title: '用印申请',
                content: params.wenjianming,
                sta: 0,
                createTime: Date.now()
            }])
            io.in(params.auditors[0].uid + '-app').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-app').emit('TASK-NEW')
            io.in(params.auditors[0].uid + '-web').emit('TASK-CHANGE')
            io.in(params.auditors[0].uid + '-web').emit('TASK-NEW')

            util.sendReminderSmsToUser(params.auditors[0].uid, '用印申请', '待审批')

            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    revertYongyinApply: async (io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc
        if (!params.id) return callback({code: -1, message: '参数缺失'})

        try {
            let res = await mysqlUtil.execute('UPDATE tb_user_yongyin_apply SET chexiao = 1 WHERE id = ? LIMIT 1', [params.id])
            if (res.affectedRows == 0) return ({code: -1, message: '撤销失败'})
            // 将审核人员的任务状态改下 并通知前端
            res = await mysqlUtil.execute('SELECT id, uid FROM tb_user_task WHERE typ = 11 AND relevenceId = ?', [params.id])
            for (let i = 0; i != res.length; i++) {
                await mysqlUtil.execute('UPDATE tb_user_task SET sta = 3 WHERE id = ? LIMIT 1', [res[i].id])
                // 通知前端
                io.in(res[i].uid + '-app').emit('TASK-REVERT', res[i].id)
                io.in(res[i].uid + '-web').emit('TASK-REVERT', res[i].id)
            }
            callback({code: 0, message: ''})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 加班申请记录
    yongyinApplyRecord: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        // 小于的id
        let lessThan = params.lessThan || 0
        // 大于的id
        let greaterThan = params.greaterThan || 0
        // 每页的数量
        let pageCount = params.pageCount || 10

        try {
            let sql = ''
            let sqlParams = []
            if (lessThan) {
                sql = 'SELECT * FROM tb_user_yongyin_apply WHERE uid = ? AND id < ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, lessThan, pageCount]
            } else if (greaterThan) {
                sql = 'SELECT * FROM tb_user_yongyin_apply WHERE uid = ? AND id > ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, greaterThan, pageCount]
            } else {
                sql = 'SELECT * FROM tb_user_yongyin_apply WHERE uid = ? ORDER BY id DESC LIMIT ?'
                sqlParams = [uid, pageCount]
            }
            let res = await mysqlUtil.execute(sql, sqlParams)
            callback({code: 0, message: 'success', data: res})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    yongyinApplyRecordWeb: async(io, socket, params, callback) => {

        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        let pageIndex = parseInt(params.pageIndex || 1);
        let pageCount = parseInt(params.pageCount || 10);

        try {
            let res = await mysqlUtil.execute('SELECT COUNT(1) cnt FROM tb_user_yongyin_apply WHERE uid = ?', [uid])
            let count = res[0].cnt
            let sql = 'SELECT * FROM tb_user_yongyin_apply WHERE uid = ? ORDER BY id DESC LIMIT ?, ?'
            let params = [uid, (pageIndex - 1) * pageCount, pageCount]
            res = await mysqlUtil.execute(sql, params)
            callback({code: 0, message: 'success', data: {list: res, count}})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    yongyinApplyDetail: async(io, socket, params, callback) => {
        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        if (!params.id) return callback({code: -1, message: '参数错误'})

        try {
            let res = await mysqlUtil.execute('SELECT * FROM tb_user_yongyin_apply WHERE id = ? LIMIT 1', [params.id])
            if (res.length == 0) return callback({code: -1, message: '不存在'})
            let applyInfo = res[0]
            applyInfo.auditors = applyInfo.auditors || []
            for (let key in applyInfo.auditors) {
                let sql = `
                    SELECT
                        con_position.name
                    FROM
                        tb_user
                    INNER JOIN con_position ON con_position.id = tb_user.position
                    WHERE tb_user.uid = ?
                    LIMIT 1
                `
                let r = await mysqlUtil.execute(sql, [applyInfo.auditors[key].uid])
                if (r.length > 0) {
                    applyInfo.auditors[key].positionName = r[0].name
                }
            }
            callback({code: 0, message: '', data: applyInfo})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 获取排班设置
    getPaibanSetting: async (io, socket, params, callback) => {

        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        try {
            let paibanSetting = {
                qiyerenyuan: [], // 企业人员
                zaibianrenyuan: [], // 在编人员
                nextqiye: '', // 下一个企业人员
                nextzaibian: '', // 下一个在编人员
                nextriqi: '', // 下一个排班日期
            }
            let sql = 'SELECT val FROM con_system_param WHERE name = "PAIBAN_SHEZHI" LIMIT 1'
            let res = await mysqlUtil.execute(sql)
            if (res.length == 0) {
                await mysqlUtil.execute('INSERT INTO con_system_param SET ?', [{name: 'PAIBAN_SHEZHI', description: '排班设置', val: JSON.stringify(paibanSetting)}])
            } else {
                paibanSetting = JSON.parse(res[0].val)
            }
            callback({code: 0, message: 'success', data: paibanSetting})
        } catch (err) {
            logger.error(err)
            callback({code:-1, message: 'system error'})
        }
    },

    // 更新排班设置
    updatePaibanSetting: async (io, socket, params, callback) => {

        let uid = socket.uid
        if (!uid) return callback({code: 9999, message: '请重新登录'})

        callback = callback || commonModule.emptyFunc

        let paibanSetting = {
            qiyerenyuan: params.qiyerenyuan || [], // 企业人员
            zaibianrenyuan: params.zaibianrenyuan || [], // 在编人员
            nextqiye: params.nextqiye || '', // 下一个企业人员
            nextzaibian: params.nextzaibian || '', // 下一个在编人员
            nextriqi: params.nextriqi || '', // 下一个排班日期
        }

        try {
            let sql = 'INSERT INTO con_system_param SET ? ON DUPLICATE KEY UPDATE ?'
            await mysqlUtil.execute(sql, [{name: 'PAIBAN_SHEZHI', val: JSON.stringify(paibanSetting), description: '排班设置'}, {val: JSON.stringify(paibanSetting)}])
            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    },

    // 自动排班
    autoPaiban: async (io, socket, params, callback) => {
        return
        logger.debug('auto paiban')
        let uid = socket.uid
        callback = callback || commonModule.emptyFunc
        if (!uid) return callback({code: 9999, message: '请重新登录'})


        try {
            let sql = 'SELECT val FROM con_system_param WHERE name = "PAIBAN_SHEZHI" LIMIT 1'
            let res = await mysqlUtil.execute(sql)
            if (res.length == 0) return callback({code: -1, message: '请先进行排班设置'})

            let paibanSetting = JSON.parse(res[0].val)

            logger.debug('paiban setting', paibanSetting)

            if (paibanSetting.qiyerenyuan.length == 0) return callback({code: -1, message: '企业人员排班列表为空'})
            if (paibanSetting.zaibianrenyuan.length == 0) return callback({code: -1, message: '在编人员排班列表为空'})

            // 生成上班状态
            res = await shengchengShangbanSta1()
            if (res.code != 0) return callback(res)

            // 排班
            // 计算从现在到下个月末的天数
            let diffDays = moment().add(1, 'month').endOf('month').diff(moment(), 'day')
            // 查询从现在到下个月末的节假日情况
            res = await mysqlUtil.execute('SELECT riqi, typ FROM con_jiejiari WHERE riqi >= ? AND riqi <= ?', [moment().format('YYYY-MM-DD'), moment().add(diffDays, 'day').format('YYYY-MM-DD')])
            let jiejiariInfo = {}
            for (let i = 0; i != res.length; i++) {
                res[i].riqi = moment(res[i].riqi).format('YYYY-MM-DD')
                if (res[i].typ == 0) { // 上班
                    jiejiariInfo[res[i].riqi] = {id: 1, desc: '上班'}
                } else if (res[i].typ == 1 || res[i].typ == 2) { // 休息
                    jiejiariInfo[res[i].riqi] = {id: 0, desc: '休息'}
                }
            }

            logger.debug('jiejiari info', jiejiariInfo)

            for (let i = 1; i != diffDays; i++) {
                let thatDay = moment().add(i, 'day').startOf('day')
                let thatDayStr = thatDay.format('YYYY-MM-DD')
                if (jiejiariInfo[thatDayStr].id == 0) { // 上次排班后的下一个休息日期
                    let zhibanUserInfo1, zhibanUserInfo2
                    logger.debug('paiban date', thatDayStr)
                    {
                        // 获取下一个在编人员的id
                        let nextzaibian = paibanSetting.nextzaibian || 0
                        let idx = paibanSetting.zaibianrenyuan.indexOf(nextzaibian)
                        if (idx == -1) {
                            idx = 0
                        } else {
                            idx++
                            if (idx >= paibanSetting.zaibianrenyuan.length) {
                                idx = 0
                            }
                        }

                        nextzaibian = paibanSetting.zaibianrenyuan[idx]
                        paibanSetting.nextzaibian = nextzaibian

                        // 更新用户的打卡信息
                        await mysqlUtil.execute('UPDATE tb_user_daka SET sta = ? WHERE uid = ? AND day = ? LIMIT 1', [JSON.stringify({id: 2, desc: '值班'}), nextzaibian, thatDayStr])

                        // 查询值班人员信息 更新到节假日表格中
                        logger.debug('next zaibian id', nextzaibian, thatDayStr)
                        zhibanUserInfo1 = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [nextzaibian])
                        zhibanUserInfo1 = zhibanUserInfo1[0]
                    }

                    {
                        // 获取下一个企业人员的id
                        let nextqiye = paibanSetting.nextqiye || 0
                        let idx = paibanSetting.qiyerenyuan.indexOf(nextqiye)
                        if (idx == -1) {
                            idx = 0
                        } else {
                            idx++
                            if (idx >= paibanSetting.qiyerenyuan.length) {
                                idx = 0
                            }
                        }

                        nextqiye = paibanSetting.qiyerenyuan[idx]
                        paibanSetting.nextqiye = nextqiye

                        // 更新用户的打卡信息
                        await mysqlUtil.execute('UPDATE tb_user_daka SET sta = ? WHERE uid = ? AND day = ? LIMIT 1', [JSON.stringify({id: 2, desc: '值班'}), nextqiye, thatDayStr])

                        // 查询值班人员信息 更新到节假日表格中
                        logger.debug('next qiye id', nextqiye, thatDayStr)
                        zhibanUserInfo2 = await mysqlUtil.execute('SELECT * FROM tb_user WHERE uid = ? LIMIT 1', [nextqiye])
                        zhibanUserInfo2 = zhibanUserInfo2[0]
                    }

                    await mysqlUtil.execute('UPDATE con_jiejiari SET zhiban = ? WHERE riqi = ? LIMIT 1', [JSON.stringify(
                        [{uid: zhibanUserInfo1.uid, name: zhibanUserInfo1.name, avatar: zhibanUserInfo1.avatar},
                        {uid: zhibanUserInfo2.uid, name: zhibanUserInfo2.name, avatar: zhibanUserInfo2.avatar}]
                        ), thatDayStr])
                }
            }

            await mysqlUtil.execute('UPDATE con_system_param SET val = ? WHERE name = "PAIBAN_SHEZHI"', [JSON.stringify(paibanSetting)])

            callback({code: 0, message: 'success'})
        } catch (err) {
            logger.error(err)
            callback({code: -1, message: 'system error'})
        }
    }
}